package com.duoketech.utils.validator.rules.string;


import com.duoketech.exception.ValidateException;
import com.duoketech.utils.validator.AbstractRule;
import org.apache.commons.lang3.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringMustBeMobile extends AbstractRule {
    private String rex = "^((13[0-9])|(15[^4])|(18[0,2,3,5-9])|(17[0-8])|(147))\\d{8}$";
//    private String rex = "^1(3[0-9]|4[57]|5[0-35-9]|7[0135678]|8[0-9])\d{8}$";//备用正则表达式

    public StringMustBeMobile(String v, String msg) {
        this.value = v;
        this.msg = msg;
    }

    @Override
    public boolean validate() throws ValidateException {
        String v = StringUtils.trim((String) this.value);
        Pattern pattern = Pattern.compile(this.rex);
        Matcher matcher = pattern.matcher(v);
        if (!matcher.matches()) {
            throw new ValidateException(this.msg);
        }
        return true;
    }
}
